package com.jackchong.net;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;

import com.jackchong.base.AppManager;
import com.jackchong.widget.LoadingDialog2;


/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/12/15 14:34
 */
public class LoadingHandler extends Handler {

    public static final int SHOW_PROGRESS_DIALOG = 1;
    public static final int HIDE_PROGRESS_DIALOG = 2;
    private static final long MIN_DURATION = 0;


    private long mStartTime;
    private Context context;
    private LoadingDialog2 mDialog;
    private static Runnable mRunnable;

    private static LoadingHandler sHandler;

    public static void show() {
        if (sHandler == null) {
            sHandler = new LoadingHandler(AppManager.getInstance().getLastActivity(), true);
        }
        sHandler.removeCallbacks(mRunnable);
    }

    public static void show(Context context) {
        if (sHandler == null) {
            sHandler = new LoadingHandler(context, true);
        }
        sHandler.removeCallbacks(mRunnable);
    }

    public static void dismiss() {
        if (sHandler != null) {
            sHandler.setEnable(false);
        }
        sHandler = null;
    }


    public LoadingHandler(Context context) {
        this(context, true);
    }

    public LoadingHandler(Context context, boolean isEnable) {
        super();
        this.context = context;
        setEnable(isEnable);
    }

    public void setOnDismissListener(@Nullable DialogInterface.OnDismissListener listener) {
    }

    private void initProgressDialog() {
        if (mDialog == null) {
            mDialog = new LoadingDialog2(context);
            if (!mDialog.isShowing() && context instanceof Activity && !((Activity) context).isFinishing()) {
                mStartTime = System.currentTimeMillis();
                mDialog.show();
            }
        }

    }

    private void dismissProgressDialog() {
        if (mDialog != null) {
            long duration = System.currentTimeMillis() - mStartTime;
            if (duration < MIN_DURATION) {
                postDelayed(this::dismissProgressDialog, MIN_DURATION - duration);
                return;
            }
            if (context instanceof Activity && !((Activity) context).isFinishing() && !mDialog.isDetached()) {
                mDialog.dismiss();
            }
            mDialog = null;
        }
    }

    public void setEnable(boolean b) {
        Message message = obtainMessage();
        message.what = b ? SHOW_PROGRESS_DIALOG : HIDE_PROGRESS_DIALOG;
        handleMessage(message);
    }


    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_PROGRESS_DIALOG:
                initProgressDialog();
                break;
            case HIDE_PROGRESS_DIALOG:
                dismissProgressDialog();
                break;
        }
    }

}
